<template>
  <div class="home">
    <el-container>
     <!-- 页面头部 -->
      <el-header>
        <div class="left">
          <img src="../assets/heima.png" alt="">
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
    <!-- 页面主体 -->
    <el-container>
     <!-- 页面左侧导航 -->
      <el-aside :width="collapse?'64px':'200px'">
        <!-- 折叠导航 -->
        <div class="toggleMenu" @click="collapse = !collapse">|||</div>
        <el-menu
        router
        :collapse="collapse"
        :collapse-transition="false"
        :default-active="activeDefault"
        unique-opened
        class="el-menu-vertical-demo"
        background-color="#333744"
        text-color="#fff"
        active-text-color="#409eff">
          <el-submenu v-for="item in menus" :key="item.id" :index="item.id + ''">
        <!-- 一级导航模板 -->
          <template slot="title">
           <!-- 图标 -->
            <i :class="iconsList[item.id]"></i>
           <!-- 文本 -->
            <span>{{item.authName}}</span>
          </template>
        <!-- 二级模板 -->
          <el-menu-item v-for="item2 in item.children" :key="item2.id" :index="'/' + item2.path">
           <!-- 图标 -->
          <i class="el-icon-menu"></i>
           <!-- 文本 -->
         <span>{{item2.authName}}</span>
       </el-menu-item>
      </el-submenu>
     </el-menu>
    </el-aside>
        <!-- 页面主体右侧内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
     </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data () {
    return {
      menus: [],
      iconsList: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      collapse: false,
      activeDefault: ''
    }
  },
  created () {
    this.getMenu()
    // 页面一进来就获取这个值
    this.activeDefault = this.$route.path
  },
  methods: {
    logout () {
      sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    async getMenu () {
      const { data: res } = await this.$axios.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.menus = res.data
    }
  }
}
</script>

<style scoped lang='less'>
.el-container,
.home {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding: 0 20px;
  align-items: center;
  .left {
      display: flex;
      align-items: center;
  }
}
.el-aside {
  background-color: #333744;
  height: 100%;
  .iconfont {
    margin-right: 8px;
  }
  .toggleMenu {
    width: 100%;
    height: 40px;
    background: skyblue;
    text-align: center;
    line-height: 40px;
    letter-spacing: 5px;
    user-select: none;
    cursor: pointer;
  }
}
.el-main {
  background-color: #eee;
  height: 100%;
}
.el-menu {
    border-right: none;
}
</style>
